package lesson;

import javax.swing.*;
import java.io.*;
import java.util.Scanner;

public class Homework {
    // 1.写一个方法，实现文件的拷贝,传两个参数
    // 2.输入一段字符串，将该字符串追加到指定文本文件中

    private Scanner sc = new Scanner(System.in);

    //文件拷贝
    public void copy(String oldFile, String newFile) {
        //文件输入
        FileInputStream input = null;
        //文件输出
        FileOutputStream output = null;
        try {
            input = new FileInputStream(oldFile);
            output = new FileOutputStream(newFile);

            int count = 0;
            byte[] bs = new byte[1024];
            while ((count = input.read(bs)) != -1) {
                System.out.println(new String(bs));
                output.write(bs, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //追加文本
    public void add() {
        FileWriter fileWriter = null;
        System.out.println("请输入要添加的内容");
        String input = sc.next();
//        JOptionPane.showInputDialog(null, input);
        try {
            fileWriter = new FileWriter("D:\\a\\txt8.txt", true);
            fileWriter.write(input);
            fileWriter.write("\n");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    public static void main(String[] args) {
        Homework homework = new Homework();
        homework.copy("D:/a/txt2.txt", "D:/a/txt3.txt");
        homework.add();
    }

}
